﻿/********************************************************************
	日期:2022/5/1 12:29
	名称:MainCityWnd
	邮箱:yunnibbd@163.com
	作者:yunni
	描述:主城UI界面
*********************************************************************/

using PEProtocol;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class MainCityWnd : WindowRoot{
    public Image imgTouch;
    public Image imgDirBg;
    public Image imgDirPoint;

    public Text txtFight;
    public Text txtPower;
    public Image imgPowerPrg;
    public Text txtLevel;
    public Text txtName;
    public Text txtExpPrg;
    public Button btnMenu;
    public Animation menuAnim;

    public Transform expPrgTrans;

    public Button btnGuide;

    private bool menuState = true;
    private Vector2 startPos = Vector2.zero;
    private Vector2 defaultPos = Vector2.zero;
    private float pointDis;

    public AutoGuideCfg curTaskData;

    protected override void InitWnd(){
        base.InitWnd();
        defaultPos = imgDirBg.transform.position;
        pointDis = Screen.height * 1.0f / Constants.ScreenStandardHeight * Constants.ScreenOpDis;
        SetActive(imgDirPoint, false);
        RegisterTouchEvents();
        RefreshUI();
    }

    public void RefreshUI(){
        PlayerData pd = GameRoot.Instance.PlayerData;
        SetText(txtFight, PECommon.GetFightByProps(pd));
        SetText(txtPower, "体力:" + pd.power + "/" + PECommon.GetPowerLimit(pd.level));
        imgPowerPrg.fillAmount = pd.power * 1.0f / PECommon.GetPowerLimit(pd.level);
        SetText(txtLevel, pd.level);
        SetText(txtName, pd.name);

        //expPrg
        int expPrgVal = (int) (pd.exp * 1.0f / PECommon.GetExpUpValByLevel(pd.level) * 100);
        SetText(txtExpPrg, expPrgVal + "%");

        int index = expPrgVal / 10;

        GridLayoutGroup grid = expPrgTrans.GetComponent<GridLayoutGroup>();

        float globalRate = 1.0f * Constants.ScreenStandardHeight / Screen.height;
        float ScreenWidth = Screen.width * globalRate;
        float width = (ScreenWidth - 180) / 10;

        grid.cellSize = new Vector2(width, 7);

        for (int i = 0; i < expPrgTrans.childCount; i++){
            Image img = expPrgTrans.GetChild(i).GetComponent<Image>();
            if (i < index){
                img.fillAmount = 1;
            }
            else if (i == index){
                img.fillAmount = expPrgVal % 10 * 1.0f / 10;
            }
            else{
                img.fillAmount = 0;
            }
        }

        //设置自动任务图标
        curTaskData = resSvc.GetAutoGuideData(pd.guideid);
        if (curTaskData != null){
            SetGuideBtnIcon(curTaskData.npcID);
        }
        else{
            SetGuideBtnIcon(-1);
        }
    }

    private void SetGuideBtnIcon(int npcID){
        string spPath = "";
        Image img = btnGuide.GetComponent<Image>();
        switch (npcID){
            case Constants.NPCWiseMan:
                spPath = PathDefine.WiseManHead;
                break;
            case Constants.NPCArtisan:
                spPath = PathDefine.ArtisanHead;
                break;
            case Constants.NPCGeneral:
                spPath = PathDefine.GeneralHead;
                break;
            case Constants.NPCTrader:
                spPath = PathDefine.TraderHead;
                break;
            default:
                spPath = PathDefine.TaskHead;
                break;
        }

        SetSprite(img, spPath);
    }

    public void ClickFuBenBtn(){
        audioSvc.PlayUIMusic(Constants.UIClickBtn);
        MainCitySys.Instance.EnterFuBen();
    }

    public void ClickStrongBtn(){
        audioSvc.PlayUIMusic(Constants.UIClickBtn);
        MainCitySys.Instance.OpenStrongWnd();
    }

    public void ClickHeadBtn(){
        audioSvc.PlayUIMusic(Constants.UIOpenPage);
        MainCitySys.Instance.OpenInfoWnd();
    }

    public void ClickMenuBtn(){
        audioSvc.PlayUIMusic(Constants.UIExtenBtn);
        menuState = (menuState ? false : true);
        AnimationClip clip = null;
        if (menuState){
            clip = menuAnim.GetClip("OpenMCMenu");
        }
        else{
            clip = menuAnim.GetClip("CloseMCMenu");
        }

        menuAnim.Play(clip.name);
    }

    public void ClickGuideBtn(){
        audioSvc.PlayUIMusic(Constants.UIClickBtn);
        if (curTaskData != null){
            MainCitySys.Instance.RunTask(curTaskData);
        }
        else{
            GameRoot.AddTips("更多引导任务，正在开发中...");
        }
    }

    public void ClickTaskBtn(){
        audioSvc.PlayUIMusic(Constants.UIClickBtn);
        MainCitySys.Instance.OpenTaskWnd();
    }

    public void ClickChatBtn(){
        audioSvc.PlayUIMusic(Constants.UIOpenPage);
        MainCitySys.Instance.OpenChatWnd();
    }

    public void ClickBuyPower(){
        audioSvc.PlayUIMusic(Constants.UIOpenPage);
        MainCitySys.Instance.OpenBuyWnd(0);
    }

    public void ClickMakeCoin(){
        audioSvc.PlayUIMusic(Constants.UIOpenPage);
        MainCitySys.Instance.OpenBuyWnd(1);
    }

    public void RegisterTouchEvents(){
        OnClickDown(imgTouch.gameObject, (PointerEventData data) => {
            imgDirBg.transform.position = data.position;
            startPos = data.position;
            SetActive(imgDirPoint);
            imgDirPoint.transform.position = data.position;
        });

        OnClickUp(imgTouch.gameObject, (PointerEventData data) => {
            imgDirBg.transform.position = defaultPos;
            SetActive(imgDirPoint, false);
            imgDirPoint.transform.localPosition = Vector2.zero;
            //TODO 方向信息传递
            MainCitySys.Instance.SetMoveDir(Vector2.zero);
        });

        OnDraw(imgTouch.gameObject, (PointerEventData data) => {
            Vector2 dir = data.position - startPos;
            float len = dir.magnitude;
            if (len > pointDis){
                Vector2 clampDir = Vector2.ClampMagnitude(dir, pointDis);
                imgDirPoint.transform.position = startPos + clampDir;
            }
            else{
                imgDirPoint.transform.position = data.position;
            }
            MainCitySys.Instance.SetMoveDir(dir.normalized);
        });
    }
    
    
}
